package com.atguigu.webadmin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.LabelInfo;
import com.atguigu.model.enums.ItemType;
import com.atguigu.webadmin.service.LabelInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "标签管理")
@RestController
@RequestMapping("/admin/label")
@CrossOrigin
public class LabelController {


    @Autowired
    private LabelInfoService labelInfoService;

    @Operation(summary = "（根据类型）查询标签列表")
    @Parameters({
            @Parameter(name = "type",description = "标签类型",in = ParameterIn.QUERY)
    })
    @GetMapping("list")
    public Result<List<LabelInfo>> labelList(@RequestParam(required = false) ItemType type) {
        //LambdaQueryWrapper:Lambda查询包装器
        LambdaQueryWrapper<LabelInfo> queryWrapper = new LambdaQueryWrapper<>();
        //条件：标签类型：先判空，再设定查询条件（LabelInfo::getType：对象中的任意元素）
        queryWrapper.eq(type != null, LabelInfo::getType, type);
        //根据标签查询列表
        List<LabelInfo> labelInfos = labelInfoService.list(queryWrapper);
        //返回结果集
        return Result.ok(labelInfos);
    }

    @Operation(summary = "新增或修改标签信息")
    @Parameters({
            @Parameter(name = "labelInfo",description = "需要新增或修改标签对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("saveOrUpdate")
    public Result<Boolean> saveOrUpdateLabel(@RequestBody LabelInfo labelInfo) {
        //调用service层方法新增或修改
        boolean b = labelInfoService.saveOrUpdate(labelInfo);
        //返回结果
        return Result.ok(b);
    }

    @Operation(summary = "根据id删除标签信息")
    @Parameters({
            @Parameter(name = "id",description = "标签id",in = ParameterIn.QUERY)
    })
    @DeleteMapping("deleteById")
    public Result<Boolean> deleteLabelById(@RequestParam Long id) {
        //根据id逻辑删除标签信息
        boolean b = labelInfoService.removeById(id);
        //返回结果
        return Result.ok(b);
    }
}
